YOU CAN PRACTICE ARRAY HERE

1. Which of the following declares a reference to an array of int named cat?
a. int cat;
b. int[] cat;
c. new int cat[];
d. int img = int[];

2. What is the output of the following code fragment:
int[] cat = {2, 4, 6, 8 };
System.out.println( cat[0] + " " + cat[1] );
 
a. 2 6
b. 8
c. 2 4
d. 6 8

3.What is the output of the following code fragment:

int[] y = new int[10];
 
y[0] = 34;
y[1] = 88;
 
System.out.println( y[0] + " " + y[1] + " " + y[5] );
 

a. 34 88 0
b. 34 88 88
c. can't compile
d. 0 34 88

4. What is the output of the following code fragment:
int[] zip = new int[5];
 
zip[0] = 7;
zip[1] = 3;
zip[2] = 4;
zip[3] = 1;
zip[4] = 9;
 
System.out.println( zip[ 2 + 1 ] );
 
a. 43
b. 37
c. 1
d. 9

5.How many objects are present after the following code fragment has executed?
double[] chris = new double[ 7 ];
double[] steve;
 
    steve = chris;
 
a. 2
b.1
c.14
d. 7

6. What is the output of the following code fragment:
int[] x = new int[9];
 
 x[0] = 7;
 x[1] = 3;
 x[2] = 4;
 
 System.out.println( (x[0] + x[1]) + " " + x[5] );
 
a. 7 3 0
b. 10 0
c. 7 3 4
d. 10 5

7. What is the output of the following code fragment:
int[] data = {0, 3, 7, 9, 12, 14};
 
     System.out.println( data[ data[1] ] );
 

a. 0
b. 3
c. 7
d.9

8. Does a programmer always know how long an array will be when the program is being written?

a.Yes---the program will not compile without the length being declared.
b.No---the array object is created when the program is running, and the length might change from run to run
c.Yes---otherwise the program will not run correctly
d.No---arrays can grow to whatever length is needed

9. What is the length of the following array: byte[]data = { 12. 34. 35. 1. 0. 6 } ?
a. 7
b. 8
c. 6
d. 12

10.) What is the output of the following code fragment:
    int[] helArray = { 2, 4, 6, 8, 10, 1, 3, 5, 7, 9 };
 
    for ( int index= 0 ; index < helArray.length ; index++  )
      System.out.print(  helArray[ index ] + "  "  );
 
a. 2 4 6 8
b. 2 4 6 8 10
c. 2 4 6 8 10 1
d. 2 4 6 8 10 1 3 5 7 9

11.) What is the length of the following array: byte[] data = { 12, 34, 9, 0, -62, 88 };
a. 5
b. 6
c. 4
d. 7

12.) Fill in the blank in the following code fragment so that each element of the array is assigned twice the value of its index. What does it prints?

int[] array = { 1, 4, 3, 6 };
int what = 0;

// scan the array
for ( int index=0; index < array.length; index++ )
{
what = what + array[ index ] ;
}
System.out.println( what );

a. 14
b. 1
c. 6
d. 1 4 3 6



CORRECT ANSWERS
1. b.
2. b.
3. a.
4. c.
5. b.
6. b.
7. d.
8. b.
9. c.
10. d.
11. b
12.a.